package com.romotc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.romotc.qqrobot.webqq.qqInterfaceCall.QQInterfaceCall;


public abstract class IpTools {

	
	/**
	 * 网络的读写锁 写的时候可以读，读的时候不能写
	 */
	private static ReentrantReadWriteLock netReadWriteLock = new ReentrantReadWriteLock();
	
	/**
	 * 网络锁
	 */
	private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	
	private static Integer lockInt = 2;
	/**
	 * 更换Ip：如果当前有读的进程 会等待读全部完成后才获取写锁(ReentrantReadWriteLock实现 读的时候不能写)
	 * 如果当前已经有现成进行写 切换到等待读状态（有其他线程换IP，我只需要坐享其成就行了）
	 * @return
	 * @throws InterruptedException 
	 */
	public static boolean  changeIp() throws InterruptedException {

		
		return true;
	}
	
	/**
	 * 请求使用网络
	 * @throws InterruptedException 
	 */
	public static void reqNet() throws InterruptedException {
		
	}
	
	/**
	 * 使用完网络之后调用
	 */
	public static void finishUseNet() {
		
	}
	/**
	 * �ж�Ip�Ƿ��Ѿ�ʹ�ù�
	 * @param wanIpValue
	 * @return
	 */
	static Properties ipList;
	public final static boolean ifInIpUsedList(String wanIpValue) throws Exception{
		return true;
	}

	public static void main(String[] args) throws Exception{

		for(int i = 0; i < 2;i++) {
			Thread t = new ReqNetThread();
			t.start();
		}
//		
		Thread t2 = new ChgIpThread();
		t2.start();
		Thread t3 = new ChgIpThread();
		t3.start();
	}
	

}

class ReqNetThread extends Thread{
	@Override
	public void run() {
		while(true)
		try {
			System.out.println("申请网络访问");
			IpTools.reqNet();
			System.out.println("成功申请网络访问");
			Thread.sleep(1000);
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			IpTools.finishUseNet();
			System.out.println("释放网络访问");
		}

	}
}

class ChgIpThread extends Thread{
	@Override
	public void run() {
		System.out.println("申请更换IP");
		while(true)
		try {
			IpTools.changeIp();
			Thread.sleep(5000);
			System.out.println("成功更换IP");
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	
	}
}